<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8'>

    <link rel="stylesheet" type="text/css" 
      href="/assets/css/straybirds.css" media="screen" />
    <link rel="stylesheet" type="text/css" 
      href="/assets/css/pygments.css" media="screen" />

    <!-- MathJax Section Start -->

    <script type="text/javascript"
    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
    </script>
    <script>
        MathJax.Hub.Config({
              tex2jax: {
              skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
              }
        });
        MathJax.Hub.Queue(function() {
            var all = MathJax.Hub.getAllJax(), i;
            for(i=0; i < all.length; i += 1) {
                all[i].SourceElement().parentNode.className += ' has-jax';
            }
        });
    </script>

    <!-- MathJax Section End -->

    <!-- Google Analytics Start-->
    <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-48100787-1', 'minixalpha.github.io');
  ga('send', 'pageview');

</script>

    <!-- Google Analytics End -->

    <title>web.py 项目架构分析之 blog</title>
  </head>

  <body>
    <div class="container">
      <header>
        <div class="container">
          <h1>
              <a href="/" title="Home Page"> 潇湘夜雨 </a>
          <span class="github-src">
            <a href ="https://github.com/minixalpha/minixalpha.github.io"
               target="_blank"
               title="Fork me on GitHub">
              <img src="/assets/images/GitHub-Mark-Light-32px.png" alt="">
            </a>
          </span>
          </h1>
        </div>
      </header>

      <aside id="left-side">
        <h2> 分类 </h2>
  <ul class="category-list">
      
            
                <li>
                <a href="/categories/计算机系统"> 计算机系统 (3) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/java"> java (1) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/技术"> 技术 (5) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/工具"> 工具 (4) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/科研"> 科研 (5) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/生活"> 生活 (1) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/思想"> 思想 (2) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/c语言"> c语言 (4) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/虚拟机"> 虚拟机 (1) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/英语"> 英语 (8) </a>
                </li>
            
      
            
                <li>
                <a href="/categories/读书"> 读书 (1) </a>
                </li>
            
      
            
                <li style="background-color: #444">
                <a href="/categories/源代码阅读"> 源代码阅读 (10) </a>
                </li>
            
      
  </ul>

      </aside>

      <aside id="right-side">
        <h2> 归档 </h2>
  <ul class="archive-list">
    
    
    
        
        
        
        
            
            <li>
                <a href="/2014/05">
                    2014-05 (1)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/04">
                    2014-04 (3)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/03">
                    2014-03 (11)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/02">
                    2014-02 (6)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2014/01">
                    2014-01 (3)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2013/11">
                    2013-11 (10)
                </a>
            </li>

        
        
    
        
        
        
        
    
        
        
        
        
    
        
        
        
        
            
            <li>
                <a href="/2013/10">
                    2013-10 (3)
                </a>
            </li>

        
        
    
  </ul>

      </aside>

      <article>

<h1>web.py 项目之 blog</h1>

<h2>目录树</h2>
<div class="highlight"><pre><code class="text language-text" data-lang="text">src/
├── blog.py
├── model.py
├── schema.sql
└── templates
    ├── base.html
    ├── edit.html
    ├── index.html
    ├── new.html
    └── view.html

1 directory, 8 files
</code></pre></div>
<h2>项目说明</h2>

<p>项目来自 <a href="http://webpy.org/src/blog/0.3">webpy.org</a>,
主要实现一个基于web的博客系统，实现了基本的增删查改。</p>

<h2>结构分析</h2>

<h3>控制模块</h3>

<p>控制模块包括 blog.py</p>
<div class="highlight"><pre><code class="python language-python" data-lang="python"><span class="sd">&quot;&quot;&quot; Basic blog using webpy 0.3 &quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">web</span>
<span class="kn">import</span> <span class="nn">model</span>

<span class="c">### Url mappings</span>

<span class="n">urls</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s">&#39;/&#39;</span><span class="p">,</span> <span class="s">&#39;Index&#39;</span><span class="p">,</span>
    <span class="s">&#39;/view/(\d+)&#39;</span><span class="p">,</span> <span class="s">&#39;View&#39;</span><span class="p">,</span>
    <span class="s">&#39;/new&#39;</span><span class="p">,</span> <span class="s">&#39;New&#39;</span><span class="p">,</span>
    <span class="s">&#39;/delete/(\d+)&#39;</span><span class="p">,</span> <span class="s">&#39;Delete&#39;</span><span class="p">,</span>
    <span class="s">&#39;/edit/(\d+)&#39;</span><span class="p">,</span> <span class="s">&#39;Edit&#39;</span><span class="p">,</span>
<span class="p">)</span>


<span class="c">### Templates</span>
<span class="n">t_globals</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;datestr&#39;</span><span class="p">:</span> <span class="n">web</span><span class="o">.</span><span class="n">datestr</span>
<span class="p">}</span>
<span class="n">render</span> <span class="o">=</span> <span class="n">web</span><span class="o">.</span><span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="s">&#39;templates&#39;</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="s">&#39;base&#39;</span><span class="p">,</span> <span class="nb">globals</span><span class="o">=</span><span class="n">t_globals</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Index</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">GET</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Show page &quot;&quot;&quot;</span>
        <span class="n">posts</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_posts</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">render</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">posts</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">View</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">GET</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; View single post &quot;&quot;&quot;</span>
        <span class="n">post</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_post</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">id</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">render</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">post</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">New</span><span class="p">:</span>

    <span class="n">form</span> <span class="o">=</span> <span class="n">web</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">Form</span><span class="p">(</span>
        <span class="n">web</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">Textbox</span><span class="p">(</span><span class="s">&#39;title&#39;</span><span class="p">,</span> <span class="n">web</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">notnull</span><span class="p">,</span> 
            <span class="n">size</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span>
            <span class="n">description</span><span class="o">=</span><span class="s">&quot;Post title:&quot;</span><span class="p">),</span>
        <span class="n">web</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">Textarea</span><span class="p">(</span><span class="s">&#39;content&#39;</span><span class="p">,</span> <span class="n">web</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">notnull</span><span class="p">,</span> 
            <span class="n">rows</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span> <span class="n">cols</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span>
            <span class="n">description</span><span class="o">=</span><span class="s">&quot;Post content:&quot;</span><span class="p">),</span>
        <span class="n">web</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="s">&#39;Post entry&#39;</span><span class="p">),</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="nf">GET</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">render</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">POST</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">form</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">form</span><span class="o">.</span><span class="n">validates</span><span class="p">():</span>
            <span class="k">return</span> <span class="n">render</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>
        <span class="n">model</span><span class="o">.</span><span class="n">new_post</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">d</span><span class="o">.</span><span class="n">title</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">d</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
        <span class="k">raise</span> <span class="n">web</span><span class="o">.</span><span class="n">seeother</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Delete</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">POST</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
        <span class="n">model</span><span class="o">.</span><span class="n">del_post</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">id</span><span class="p">))</span>
        <span class="k">raise</span> <span class="n">web</span><span class="o">.</span><span class="n">seeother</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Edit</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">GET</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
        <span class="n">post</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_post</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">id</span><span class="p">))</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">New</span><span class="o">.</span><span class="n">form</span><span class="p">()</span>
        <span class="n">form</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">post</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">render</span><span class="o">.</span><span class="n">edit</span><span class="p">(</span><span class="n">post</span><span class="p">,</span> <span class="n">form</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">POST</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">New</span><span class="o">.</span><span class="n">form</span><span class="p">()</span>
        <span class="n">post</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_post</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">id</span><span class="p">))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">form</span><span class="o">.</span><span class="n">validates</span><span class="p">():</span>
            <span class="k">return</span> <span class="n">render</span><span class="o">.</span><span class="n">edit</span><span class="p">(</span><span class="n">post</span><span class="p">,</span> <span class="n">form</span><span class="p">)</span>
        <span class="n">model</span><span class="o">.</span><span class="n">update_post</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">id</span><span class="p">),</span> <span class="n">form</span><span class="o">.</span><span class="n">d</span><span class="o">.</span><span class="n">title</span><span class="p">,</span> <span class="n">form</span><span class="o">.</span><span class="n">d</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
        <span class="k">raise</span> <span class="n">web</span><span class="o">.</span><span class="n">seeother</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>


<span class="n">app</span> <span class="o">=</span> <span class="n">web</span><span class="o">.</span><span class="n">application</span><span class="p">(</span><span class="n">urls</span><span class="p">,</span> <span class="nb">globals</span><span class="p">())</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</code></pre></div>
<p>可以看出，主要的逻辑，即对各个请求的处理，都在这个模块中，
包括表单的生成，数据的获取，以及对模板引擎的调用。这个
模块主要是在顶层调用，具体的细节不在这里实现。</p>

<h3>显示模块</h3>

<p>显示模块没有具体实现逻辑的代码，只有模板引擎用到的一些HTML
文件，这些文件的大体内容已经定下来，只是具体需要填充的内容
需要在控制模块中进行设定。</p>

<p>templates/base.html  </p>
<div class="highlight"><pre><code class="html language-html" data-lang="html">$def with (page)

<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;title&gt;</span>My Blog<span class="nt">&lt;/title&gt;</span>
    <span class="nt">&lt;style&gt;</span>
        <span class="nf">#menu</span> <span class="p">{</span>
            <span class="k">width</span><span class="o">:</span> <span class="m">200px</span><span class="p">;</span>
            <span class="k">float</span><span class="o">:</span> <span class="k">right</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="nt">&lt;/style&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>

<span class="nt">&lt;ul</span> <span class="na">id=</span><span class="s">&quot;menu&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/&quot;</span><span class="nt">&gt;</span>Home<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
    <span class="nt">&lt;li&gt;&lt;a</span> <span class="na">href=</span><span class="s">&quot;/new&quot;</span><span class="nt">&gt;</span>New Post<span class="nt">&lt;/a&gt;&lt;/li&gt;</span>
<span class="nt">&lt;/ul&gt;</span>

$:page

<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</code></pre></div>
<p><code>base.html</code> 为每个文件提供都基本的模板，其它文件只负责自己的那
一块内容。</p>

<p>templates/index.html</p>
<div class="highlight"><pre><code class="html language-html" data-lang="html">$def with (posts)

<span class="nt">&lt;h1&gt;</span>Blog posts<span class="nt">&lt;/h1&gt;</span>

<span class="nt">&lt;ul&gt;</span>
$for post in posts:
    <span class="nt">&lt;li&gt;</span>
        <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;/view/$post.id&quot;</span><span class="nt">&gt;</span>$post.title<span class="nt">&lt;/a&gt;</span> 
        from $datestr(post.posted_on) 
        <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">&quot;/edit/$post.id&quot;</span><span class="nt">&gt;</span>Edit<span class="nt">&lt;/a&gt;</span>
    <span class="nt">&lt;/li&gt;</span>
<span class="nt">&lt;/ul&gt;</span>
</code></pre></div>
<p><code>index.html</code> 负责文章列表的显示。</p>

<p>templates/new.html  </p>
<div class="highlight"><pre><code class="html language-html" data-lang="html">$def with (form)


<span class="nt">&lt;h1&gt;</span>New Blog Post<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">&quot;&quot;</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span><span class="nt">&gt;</span>
$:form.render()
<span class="nt">&lt;/form&gt;</span>
</code></pre></div>
<p><code>new.html</code> 负责新建文件页面。</p>

<p>templates/edit.html</p>
<div class="highlight"><pre><code class="html language-html" data-lang="html">$def with (post, form)

<span class="nt">&lt;h1&gt;</span>Edit $form.d.title<span class="nt">&lt;/h1&gt;</span>

<span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">&quot;&quot;</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span><span class="nt">&gt;</span>
$:form.render()
<span class="nt">&lt;/form&gt;</span>


<span class="nt">&lt;h2&gt;</span>Delete post<span class="nt">&lt;/h2&gt;</span>
<span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">&quot;/delete/$post.id&quot;</span> <span class="na">method=</span><span class="s">&quot;post&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;submit&quot;</span> <span class="na">value=</span><span class="s">&quot;Delete post&quot;</span><span class="nt">/&gt;</span>
<span class="nt">&lt;/form&gt;</span>
</code></pre></div>
<p><code>edit.html</code> 负责文章的编辑页面。</p>

<p>templates/view.html</p>
<div class="highlight"><pre><code class="html language-html" data-lang="html">$def with (post)

<span class="nt">&lt;h1&gt;</span>$post.title<span class="nt">&lt;/h1&gt;</span>
$datestr(post.posted_on)<span class="nt">&lt;br/&gt;</span>

$post.content
</code></pre></div>
<p><code>view.html</code> 负责显示文章页面。</p>

<h3>数据模块</h3>

<p>数据模块包含两个文件　<code>schema.sql</code> 和 <code>model.py</code></p>

<p>schema.sql  </p>
<div class="highlight"><pre><code class="text language-text" data-lang="text">CREATE TABLE entries (
    id INT AUTO_INCREMENT,
    title TEXT,
    content TEXT,
    posted_on DATETIME,
    primary key (id)
);
</code></pre></div>
<p><code>schema.sql</code> 包含了数据库中表的格式</p>

<p>model.py</p>
<div class="highlight"><pre><code class="python language-python" data-lang="python"><span class="kn">import</span> <span class="nn">web</span><span class="o">,</span> <span class="nn">datetime</span>

<span class="n">db</span> <span class="o">=</span> <span class="n">web</span><span class="o">.</span><span class="n">database</span><span class="p">(</span><span class="n">dbn</span><span class="o">=</span><span class="s">&#39;mysql&#39;</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="s">&#39;miniblog&#39;</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="s">&#39;root&#39;</span><span class="p">,</span> <span class="n">pw</span><span class="o">=</span><span class="s">&quot;7102155&quot;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">get_posts</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">db</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s">&#39;entries&#39;</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s">&#39;id DESC&#39;</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">get_post</span><span class="p">(</span><span class="nb">id</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">db</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s">&#39;entries&#39;</span><span class="p">,</span> <span class="n">where</span><span class="o">=</span><span class="s">&#39;id=$id&#39;</span><span class="p">,</span> <span class="nb">vars</span><span class="o">=</span><span class="nb">locals</span><span class="p">())[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">None</span>

<span class="k">def</span> <span class="nf">new_post</span><span class="p">(</span><span class="n">title</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
    <span class="n">db</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="s">&#39;entries&#39;</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="n">title</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="n">text</span><span class="p">,</span> <span class="n">posted_on</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">())</span>

<span class="k">def</span> <span class="nf">del_post</span><span class="p">(</span><span class="nb">id</span><span class="p">):</span>
    <span class="n">db</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s">&#39;entries&#39;</span><span class="p">,</span> <span class="n">where</span><span class="o">=</span><span class="s">&quot;id=$id&quot;</span><span class="p">,</span> <span class="nb">vars</span><span class="o">=</span><span class="nb">locals</span><span class="p">())</span>

<span class="k">def</span> <span class="nf">update_post</span><span class="p">(</span><span class="nb">id</span><span class="p">,</span> <span class="n">title</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
    <span class="n">db</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s">&#39;entries&#39;</span><span class="p">,</span> <span class="n">where</span><span class="o">=</span><span class="s">&quot;id=$id&quot;</span><span class="p">,</span> <span class="nb">vars</span><span class="o">=</span><span class="nb">locals</span><span class="p">(),</span>
        <span class="n">title</span><span class="o">=</span><span class="n">title</span><span class="p">,</span> <span class="n">content</span><span class="o">=</span><span class="n">text</span><span class="p">)</span>
</code></pre></div>
<p><code>model.py</code> 包含了数据库连接的创建与数据库操作的具体细节。
此模块中的函数被控制模块调用，从数据库中读取数据。</p>

<p>总的来说，控制模块在最顶层，负责对请求进行处理，调用数据模块，
从数据库中读取，写入数据。再将从数据库中取出的数据送给显示模块
进行显示。</p>

<p>感觉项目再大一点就应该把控制模块再分割了。</p>


      </article>

      <div class="comments">
        
          <div id="disqus_thread"></div>
 <script type="text/javascript">
     /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
     var disqus_shortname = 'minixalpha'; // required: replace example with your forum shortname

     /* * * DON'T EDIT BELOW THIS LINE * * */
     (function() {
         var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
         dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
         (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
     })();
 </script>
 <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
    

        
      </div>


      <footer>
        Copyright (c) minixalpha 2014
      </footer>

    </div>
  </body>
</html>
